USE [iCoffee]
GO

/****** Object:  StoredProcedure [dbo].[usp_LST_Product]    Script Date: 05/24/2012 08:55:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE Proc [dbo].[usp_LST_Product]
	@Command nvarchar(255),
	@ProductID	INT=NULL output,
	@Code	nvarchar(50)=NULL,
	@Name	nvarchar(200) = '',
	@UnitID	INT=NULL,
	@Type	int = 1,
	@GroupID	INT=NULL,
	@Notes	nvarchar(200)=NULL
AS
Begin

	If @Command = 'Update'
	Begin
		If Not Exists(Select ProductID From LST_Product Where ProductID = @ProductID )
		Begin
			Insert Into LST_Product(Code,Name,UnitID,Type,GroupID,Notes)
			Values(@Code,@Name,@UnitID,@Type,@GroupID,@Notes)
			Set @ProductID = @@identity
		End
		Else
			Update LST_Product Set Code = @Code,Name = @Name,UnitID = @UnitID,Type = @Type,GroupID = @GroupID,Notes = @Notes
			Where ProductID = @ProductID 
	End

	If @Command = 'Delete'
	Begin
		Delete From LST_Product
		Where ProductID = @ProductID 
	End

	If @Command = 'LoadData'
	Begin
		Select * From LST_Product
		Where ProductID = @ProductID 
	END
	
	If @Command = 'GetAll'
	Begin
		Select * From LST_Product
	End
End

GO

